//
//  MangaTableViewCell.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MangaTableViewCell.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
//#define HEIGHT 250
@implementation MangaTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.mangaData = [NSMutableArray array];
        
        self.mangaTitle = [[UILabel alloc] init];
        [self.contentView addSubview:self.mangaTitle];
        [self.mangaTitle release];
        
        self.moreButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:self.moreButton];
        [self.moreButton setImage:[UIImage imageNamed:@"iconfont-zhankai"] forState:UIControlStateNormal];
        
        //layout
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        //150  166
        CGFloat bili = 166.0 / 150;
        
        flow.itemSize = CGSizeMake(WIDTH / 3.5, WIDTH / 3.5 * bili + 30);
        flow.minimumInteritemSpacing = 10;
        flow.minimumLineSpacing = 10;
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //边距
        flow.sectionInset = UIEdgeInsetsMake(5, 15, 5, 10);
        
        self.mangaCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, WIDTH / 3.5 * bili + 40) collectionViewLayout:flow];
        [self.contentView addSubview:self.mangaCollection];
        self.mangaCollection.delegate = self;
        self.mangaCollection.dataSource = self;
        //注册
        [self.mangaCollection registerClass:[MangaCollectionViewCell class] forCellWithReuseIdentifier:@"reuseCollectionManga"];
        self.mangaCollection.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    mangaData *md = self.mangaData[indexPath.row];
    [self.delegate getMangaComicID:md.comic_id];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.mangaTitle.frame = CGRectMake(0, 0, WIDTH, 40);
    self.mangaTitle.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1];
    self.mangaTitle.layer.borderWidth = 5;
    self.mangaTitle.layer.cornerRadius = 10;
    self.mangaTitle.layer.borderColor = [UIColor clearColor].CGColor;
    
    
    self.moreButton.frame = CGRectMake(WIDTH - self.moreButton.imageView.image.size.width - 5, 5, self.moreButton.imageView.image.size.width, self.moreButton.imageView.image.size.height);
    [self.moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)moreButtonAction:(UIButton *)button{
    self.myMoreBlock(self.comic_list_id);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.mangaData.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MangaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCollectionManga" forIndexPath:indexPath];
    mangaData *md = self.mangaData[indexPath.row];
    NSURL *url = [NSURL URLWithString:md.cover_url];
    UIImage *placeImage = [UIImage imageNamed:@"FOR@{XC4(JCI7}{P@Y]({{1－1（被拖移）.tiff"];
    [cell.MangaImage sd_setImageWithURL:url placeholderImage:placeImage];
    cell.imageLabel.text = md.title;
    return cell;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
